<template>
    <div class="add-user-container">
        <!-- 页面标题区域 -->
        <div class="page-header">
            <div class="header-content">
                <div class="title-section">
                    <el-icon class="title-icon" size="28">
                        <User />
                    </el-icon>
                    <h1 class="page-title">添加用户</h1>
                </div>
                <p class="page-description">创建新用户并分配相应角色权限</p>
            </div>
        </div>

        <!-- 表单卡片容器 -->
        <div class="form-container">
            <el-card class="form-card" shadow="hover">
                <template #header>
                    <div class="card-header">
                        <el-icon class="card-icon" size="20">
                            <Edit />
                        </el-icon>
                        <span class="card-title">用户信息</span>
                    </div>
                </template>

                <el-form ref="ruleFormRef" class="user-form" :model="ruleForm" :rules="rules" label-width="100px"
                    label-position="left">
                    <!-- 用户名输入 -->
                    <el-form-item label="用户名" prop="userName" class="form-item">
                        <el-input v-model="ruleForm.userName" placeholder="请输入用户名" clearable class="form-input">
                            <template #prefix>
                                <el-icon class="input-icon">
                                    <User />
                                </el-icon>
                            </template>
                        </el-input>
                    </el-form-item>

                    <!-- 密码输入 -->
                    <el-form-item label="密码" prop="password" class="form-item">
                        <el-input v-model="ruleForm.password" type="password" placeholder="请输入密码" show-password
                            clearable class="form-input">
                            <template #prefix>
                                <el-icon class="input-icon">
                                    <Lock />
                                </el-icon>
                            </template>
                        </el-input>
                    </el-form-item>

                    <!-- 昵称输入 -->
                    <el-form-item label="昵称" prop="nickName" class="form-item">
                        <el-input v-model="ruleForm.nickName" placeholder="请输入昵称" clearable class="form-input">
                            <template #prefix>
                                <el-icon class="input-icon">
                                    <Avatar />
                                </el-icon>
                            </template>
                        </el-input>
                    </el-form-item>

                    <!-- 角色选择 -->
                    <el-form-item label="角色" prop="roleIds" class="form-item role-section">
                        <div class="role-container">
                            <el-checkbox-group v-model="ruleForm.roleIds" class="role-checkbox-group">
                                <el-checkbox v-for="item in roleOptions" :key="item.roleId" :label="item.roleId"
                                    :value="item.roleId" class="role-checkbox">
                                    <div class="role-item">
                                        <el-icon class="role-icon">
                                            <UserFilled />
                                        </el-icon>
                                        <span class="role-name">{{ item.roleName }}</span>
                                    </div>
                                </el-checkbox>
                            </el-checkbox-group>
                        </div>
                    </el-form-item>

                    <!-- 按钮区域 -->
                    <el-form-item class="button-section">
                        <div class="button-group">
                            <el-button type="primary" size="large" :loading="isSubmitting"
                                @click="submitForm(ruleFormRef)" class="submit-btn">
                                <el-icon class="btn-icon">
                                    <Check />
                                </el-icon>
                                创建用户
                            </el-button>
                            <el-button size="large" @click="resetForm(ruleFormRef)" class="reset-btn">
                                <el-icon class="btn-icon">
                                    <Refresh />
                                </el-icon>
                                重置表单
                            </el-button>
                        </div>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import {
    User,
    Edit,
    Lock,
    Avatar,
    UserFilled,
    Check,
    Refresh
} from '@element-plus/icons-vue'
import { read, write } from '@/Until/axiosHepler'

interface RuleForm {
    userName: string
    password: string
    roleIds: []
    nickName: string

}

const ruleFormRef = ref<FormInstance>()
const isSubmitting = ref(false)
const ruleForm = reactive<RuleForm>({
    userName: '',
    password: '',
    roleIds: [],
    nickName: ''
})
const roleOptions = ref<any[]>([])

const roleList = () => {
    read.get("api/User/RoleList").then((res) => {
        roleOptions.value = res.data.data;
    })
}

onMounted(() => {
    roleList();
})

const rules = reactive<FormRules<RuleForm>>({
    userName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ],
    nickName: [
        { required: true, message: '请输入昵称', trigger: 'blur' },
    ],

})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return

    try {
        await formEl.validate(async (valid, fields) => {
            if (valid) {
                isSubmitting.value = true
                try {
                    const res = await write.post("/api/Manger/AddUserRole", ruleForm)
                    if (res.data.code == 200) {
                        ElMessage.success("添加用户成功")
                        resetForm(formEl)
                    } else {
                        ElMessage.error(res.data.message || "添加用户失败")
                    }
                } catch (error) {
                    console.error('提交失败:', error)
                    ElMessage.error("网络错误，请稍后重试")
                } finally {
                    isSubmitting.value = false
                }
            } else {
                ElMessage.warning("请检查表单填写是否完整")
            }
        })
    } catch (error) {
        console.error('表单验证失败:', error)
    }
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>

<style scoped>
.add-user-container {
    min-height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.page-header {
    width: 100%;
    max-width: 800px;
    margin-bottom: 30px;
    animation: fadeInDown 0.6s ease-out;
}

.header-content {
    text-align: center;
    color: white;
}

.title-section {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 10px;
}

.title-icon {
    color: #ffd700;
    animation: pulse 2s infinite;
}

.page-title {
    font-size: 2.5rem;
    font-weight: 700;
    margin: 0;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.page-description {
    font-size: 1.1rem;
    opacity: 0.9;
    margin: 0;
    font-weight: 300;
}

.form-container {
    width: 100%;
    max-width: 800px;
    animation: fadeInUp 0.8s ease-out;
}

.form-card {
    border-radius: 16px;
    border: none;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
    background: rgba(255, 255, 255, 0.95);
}

.card-header {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    color: #2c3e50;
}

.card-icon {
    color: #667eea;
}

.card-title {
    font-size: 1.2rem;
}

.user-form {
    padding: 20px 0;
}

.form-item {
    margin-bottom: 24px;
}

.form-input {
    transition: all 0.3s ease;
}

.form-input:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
}

.input-icon {
    color: #667eea;
    transition: color 0.3s ease;
}

.form-input:focus-within .input-icon {
    color: #764ba2;
}

.role-section {
    margin-bottom: 32px;
}

.role-container {
    background: #f8f9fa;
    border-radius: 12px;
    padding: 20px;
    border: 2px solid #e9ecef;
    transition: all 0.3s ease;
}

.role-container:hover {
    border-color: #667eea;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.1);
}

.role-checkbox-group {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
}

.role-checkbox {
    margin: 0;
    transition: all 0.3s ease;
}

.role-checkbox:hover {
    transform: translateY(-2px);
}

.role-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: white;
    border-radius: 8px;
    border: 2px solid #e9ecef;
    transition: all 0.3s ease;
    cursor: pointer;
}

.role-checkbox:hover .role-item {
    border-color: #667eea;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.15);
}

.role-checkbox.is-checked .role-item {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: white;
    border-color: #667eea;
}

.role-icon {
    font-size: 16px;
}

.role-name {
    font-weight: 500;
    font-size: 14px;
}

.button-section {
    margin-top: 40px;
    margin-bottom: 0;
}

.button-group {
    display: flex;
    gap: 16px;
    justify-content: center;
    flex-wrap: wrap;
}

.submit-btn {
    background: linear-gradient(135deg, #667eea, #764ba2);
    border: none;
    border-radius: 8px;
    padding: 12px 32px;
    font-weight: 600;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.submit-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.submit-btn:active {
    transform: translateY(0);
}

.reset-btn {
    background: #f8f9fa;
    border: 2px solid #e9ecef;
    color: #6c757d;
    border-radius: 8px;
    padding: 12px 32px;
    font-weight: 600;
    transition: all 0.3s ease;
}

.reset-btn:hover {
    background: #e9ecef;
    border-color: #dee2e6;
    color: #495057;
    transform: translateY(-2px);
}

.btn-icon {
    margin-right: 6px;
}

/* 动画效果 */
@keyframes fadeInDown {
    from {
        opacity: 0;
        transform: translateY(-30px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.1);
    }
}

/* 响应式设计 */
@media (max-width: 768px) {
    .add-user-container {
        padding: 15px;
    }

    .page-title {
        font-size: 2rem;
    }

    .page-description {
        font-size: 1rem;
    }

    .form-card {
        margin: 0;
    }

    .role-checkbox-group {
        grid-template-columns: 1fr;
    }

    .button-group {
        flex-direction: column;
        align-items: center;
    }

    .submit-btn,
    .reset-btn {
        width: 100%;
        max-width: 300px;
    }
}

@media (max-width: 480px) {
    .title-section {
        flex-direction: column;
        gap: 8px;
    }

    .page-title {
        font-size: 1.8rem;
    }

    .role-container {
        padding: 15px;
    }

    .role-item {
        padding: 10px 12px;
    }
}
</style>